<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>发布订阅模式</title>
</head>
<body>
    
</body>
</html>
<script>
    var salesOffices = {}; //定义售楼处

    salesOffices.clientList = {}; //缓存列表，存放订阅者的回调函数

    salesOffices.listen = function(key,fn){
        this.clientList[key] = this.clientList[key] || [];
        this.clientList[key].push(fn);
    }
    salesOffices.trigger = function(){
        var key = Array.prototype.shift.call(arguments),
            fns = this.clientList[key];

        if(!fns ||  fns.length === 0){
            return false;
        }    
        for(var i =0, len = fns.length;i<len;i++){
            fns[i].apply(this,arguments);
        }
    };
    //test
    salesOffices.listen("zap",function(price,squareMeter){
       console.log(price,squareMeter);
    });
    salesOffices.listen("sxj",function(price,squareMeter){
       console.log(price,squareMeter);
    });


    salesOffices.trigger("zap","147万","90平米");
    salesOffices.trigger("sxj","160万","100平米");

</script>